<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Service\Facades\Pay;
use App\Service\Model\Cards;
use App\Service\Model\Addcus;
use App\User;
use App\Service\Model\DayTotal;

class CardsController extends BaseController
{
    public function __construct()
    {
    }

    /**
     * @param Request $request
     */
    public function lists(Request $request)
    {
        /** @var User $user */
        $user = auth()->user();

        return $this->success($user->cards);
    }


    /**
     * 进件接口
     *
     * @param Request $request
     * @return void
     */
    public function addcus(Request $request){
        $rules        = [
            'cardno'        => 'bail|required',
            'acctname'      => 'bail|required',
            'tel'           => 'bail|required',
            'idno'          => 'bail|required',
            'province_code' => 'bail|required',
            'city_code'     => 'bail|required',
            'province'      => 'bail|required',
            'city'          => 'bail|required',
            'address'       => 'bail|required'
        ];
        $this->validate($request,$rules);
        $cardno       = $request->get('cardno');          //卡号
        $acctname     = $request->get('acctname');        //账户名
        $tel          = $request->get('tel');             //电话号码
        $idno         = $request->get('idno');            //身份证
        $provinceCode = $request->get('province_code'); //省份代码
        $cityCode     = $request->get('city_code');        //城市代码
        $address      = $request->get('address');
        /** @var User $user */
        $user       = user();
        $outcusid   = 'outcusid-'.$user->getKey();
        $addcus     = $user->addcus;
        if($addcus){
           //return $this->failed('进件信息已存在');
        }
        $ret    = Pay::addcus($outcusid , $acctname , $idno , $tel , $cardno , $provinceCode , $cityCode ,$address);
        if(Pay::check($ret)){
            $user->cusid    = $ret->cusid;
            $user->province = $request->province;
            $user->city     = $request->city;
            $user->name     = $acctname;
            $user->status   = User::status_verified;
            $user->idno     = $idno ;
            $user->save();
            Addcus::create([
                'province'  => $provinceCode,
                'city'      => $cityCode,
                'cusid'     => $ret->cusid,
                'uid'       => $user->getKey(),
                'ret'       => objectToString($ret),
                'name'      => $acctname,
                'idno'      => $idno,
                'outcusid'  => $outcusid,
            ]);
            return $this->success();
        }else{
            return $this->failed('操作失败:'.$ret->retmsg);
        }
    }
    
    

    /**
     * 绑定信用卡
     *
     * @param Request $request
     */
    public function bind(Request $request)
    {
        $rules = [
            'cardno'    => 'bail|required',
            'acctname'  => 'bail|required',
            'validdate' => 'bail|required',
            'cvv2'      => 'bail|required',
            'tel'       => 'bail|required|regex:/^1[34578]\d{9}$/',
            'idno'      => 'bail|required',
            'bankName'  => 'bail|required',
        ];

        $this->validate($request, $rules, [], Cards::attribute);
        $cusid      = user()->cusid;
        $cardno     = $request->get('cardno'); //卡号
        $acctname   = $request->get('acctname'); //账户名
        $validdate  = $request->get('validdate'); //有效期
        $cvv2       = $request->get('cvv2'); //cvv2
        $tel        = $request->get('tel'); //电话号码
        $idno       = $request->get('idno'); //身份证
        $payDate    = $request->get('payDate'); //还款日
        $billDate   = $request->get('billDate'); //账单日
        $bankName   = $request->bankName;

        $card       = Cards::getCardByNo($cardno);
        if(!is_null($card)){
            if($card->trxstatus == '0000'){
                return $this->failed('该卡已绑定成功');
            }
            if($card->trxstatus == '1999'){
                $card->delete();
            }
        }
        
        
        
        /** @var User $user */
        $user =  user();
        $card = Cards::create([
            'uid'       => $user->getKey(),
            'cusid'     => $user->cusid,
            'cardno'    => $cardno,
            'acctname'  => $acctname,
            'accttype'  => Cards::credit_card,
            'validdate' => $validdate,
            'cvv2'      => $cvv2,
            'tel'       => $tel,
            'idno'      => $idno,
            'bank_name' => $bankName,
            'agency_id' => $user->agency_id,
        ]);
        
        $ret = Pay::bind($cusid , $cardno, $acctname, $validdate, $cvv2, $tel, $idno);

        if (Pay::check($ret)) {
            
            $card->ret       = objectToString($ret);
            $card->trxstatus = $ret->trxstatus;
            $card->status    = Cards::status_unconfirm;
            $card->saveOrFail();
            //两种可能 :   code = 1999  则已经发送验证码  需要调用确认绑卡接口
            //            code = 0000 是直接绑卡成功
            return $this->success(['status' => $ret->trxstatus]);
        } else {
            $card->delete();
            return $this->failed($ret->retmsg);
        }
    }

    /**
     * 确认绑卡
     *
     * @param Request $request
     */
    public function confirm(Request $request)
    {
        $rules = [
            'cardno'  => 'bail|required|exists:cards,cardno',
            'smscode' => 'bail|required',
        ];
        $this->validate($request, $rules, [], Cards::attribute);

        $cardno = $request->get('cardno');
        $smscode = $request->get('smscode');
        $card = Cards::getCardByNo($cardno);
        $ret = Pay::confirm(
            $card->cardno,
            $card->acctname,
            $card->validdate,
            $card->cvv2,
            $card->tel,
            $card->idno,
            $smscode,
            $card->cusid,
            $card->uid
        );
        if (Pay::check($ret)) {
            $card->trxstatus = $ret->trxstatus;
            $card->agreeid   = $ret->agreeid;
            $card->status    = Cards::status_success;
            $card->saveOrFail();
            
            
            
            if($card->agency_id != 0){
                DayTotal::incr($card->agency_id,'total_bind');
            }
            
            return $this->success();
        }

        return $this->failed('绑卡确认失败'.$ret->retmsg);
    }

    /**
     * 重发验证码
     *
     * @param Request $request
     */
    public function resendSmsCode(Request $request)
    {
        $rules = [
            'cardno' => 'bail|required|exists:cards,cardno',
        ];
        $this->validate($request, $rules, [], Cards::attribute);

        $cardno = $request->get('cardno');
        $card = Cards::getCardByNo($cardno);

        $ret = Pay::resendSmsCode(
            $card->cardno,
            $card->acctname,
            $card->validdate,
            $card->cvv2,
            $card->tel,
            $card->idno,
            $card->cusid,
            $card->uid
        );
        if (Pay::check($ret)) {
            return $this->success();
        }
        return $this->failed('发送失败');
    }

    /**
     * 解绑银行卡
     *
     * @param Request $request
     */
    public function unbind(Request $request)
    {
        $rules = [
            'cardno' => 'bail|required|exists:cards,cardno',
        ];
        $this->validate($request, $rules, [], Cards::attribute);
        $cardno = $request->get('cardno');
        /** @var Cards $card */
        $card   = Cards::getCardByNo($cardno);
        if(empty($card)){
            return $this->failed('卡号已解绑或不存在');
        }
        if($card->uid != user()->getKey()){
            return $this->failed('只能解绑本人银行卡');
        }
        if($card->plan_status == Cards::plan_status_on){
            return $this->failed('正在执行计划,无法解绑');
        }
        $cusid      = user()->cusid;
        $ret = Pay::unbind($cusid , $cardno);
        if (Pay::check($ret)) {
            $card->is_delete = 1;
            $card->status    = Cards::status_delete;
            $card->save();
            return $this->success();
        }
        return $this->failed('解绑失败:'.$ret->retmsg);
    }
    
}
